﻿using Coldairarrow.Entity.Base;
using Coldairarrow.Entity.Base_Manage;
using Coldairarrow.Entity.DTO;
using Coldairarrow.IBusiness;
using Coldairarrow.Util;
using EFCore.Sharding;
using LinqKit;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Linq.Expressions;
using System.Threading.Tasks;

namespace Coldairarrow.Business.Base
{
    public partial class Base_RepairOrderAccidentBusiness : BaseBusiness<Base_RepairOrderAccident>, IBase_RepairOrderAccidentBusiness, ITransientDependency
    {
        readonly IOperator _operator;
        public Base_RepairOrderAccidentBusiness(IDbAccessor db, IOperator @operator)
            : base(db)
        {
            _operator = @operator;
        }

        #region 外部接口

        public async Task<PageResult<Base_RepairOrderAccident>> GetDataListAsync(PageInput<ConditionDTO> input)
        {
            var q = GetIQueryable();
            var where = LinqHelper.True<Base_RepairOrderAccident>();
            var search = input.Search;

            //筛选
            if (!search.Condition.IsNullOrEmpty() && !search.Keyword.IsNullOrEmpty())
            {
                var newWhere = DynamicExpressionParser.ParseLambda<Base_RepairOrderAccident, bool>(
                    ParsingConfig.Default, false, $@"{search.Condition}.Contains(@0)", search.Keyword);
                where = where.And(newWhere);
            }

            return await q.Where(where).GetPageResultAsync(input);
        }

        public async Task<Base_RepairOrderAccident> GetTheDataAsync(string id)
        {
            return await GetEntityAsync(id);
        }

        public async Task AddDataAsync(Base_RepairOrderAccident data)
        {
            await InsertAsync(data);
        }

        public async Task UpdateDataAsync(Base_RepairOrderAccident data)
        {
            await UpdateAsync(data);
        }

        public async Task DeleteDataAsync(List<string> ids)
        {
            await DeleteAsync(ids);
        }
        /// <summary>
        /// Pc获取设备事故列表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PageResult<Base_RepairOrderAccidentDTO>> GetRepairOrderApplyList(PageInput<PB_ScheduleDetailConditionDTO> input)
        {
            var where = LinqHelper.True<Base_RepairOrderAccidentDTO>();
            var search = input.Search;
            Expression<Func<Base_RepairOrderAccident, Base_Department,Base_Device, Base_RepairOrderAccidentDTO>> select = (a, b, c) => new Base_RepairOrderAccidentDTO
            {
                DeviceName=c.Name,
                AccidentDepartmentName=b.Name,
            };
            select = select.BuildExtendSelectExpre();
            var q = from a in GetIQueryable().AsExpandable()
                    join b in Db.GetIQueryable<Base_Department>() on a.AccidentDepartment equals b.Id into ab
                    from b in ab.DefaultIfEmpty()
                    join c in Db.GetIQueryable<Base_Device>() on a.DeviceId equals c.Id into ac
                    from c in ac.DefaultIfEmpty()
                    select @select.Invoke(a, b, c);
            if (!input.Search.Name.IsNullOrEmpty()) {
                q = q.Where(x=>x.DeviceName.Contains(input.Search.Name));
            }
            if (!search.StartDate.IsNullOrEmpty() && !search.EndDate.IsNullOrEmpty())
            {
                DateTime StartDate = Convert.ToDateTime(Convert.ToDateTime(search.StartDate).Date.ToString());
                DateTime EndDate = Convert.ToDateTime(Convert.ToDateTime(search.EndDate).Date.ToString()).AddDays(1).AddSeconds(-1);
                q = q.Where(x => StartDate <= x.DateofAccident && x.DateofAccident <= EndDate);
            }
            q = q.Where(m=>_operator.Property.DeptIds.Contains(m.AccidentDepartment));
            return await q.Where(where).GetPageResultAsync(input);
        }
        /// <summary>
        /// 获取设备规格
        /// </summary>
        /// <param name="DeviceId"></param>
        /// <returns></returns>
        public async Task<Base_RepairOrderAccidentSpec> GetRepairOrderAccidentSpec(string DeviceId)
        {
            var a =await Db.GetIQueryable<Base_Device>().Where(m=>m.Id==DeviceId).FirstOrDefaultAsync();
            Base_RepairOrderAccidentSpec base_RepairOrderAccidentSpec = new Base_RepairOrderAccidentSpec();
            base_RepairOrderAccidentSpec.Id = a.Id;
            base_RepairOrderAccidentSpec.Spec = a.Spec;
            return base_RepairOrderAccidentSpec;
        }
        /// <summary>
        /// 通过设备编号获取设备事故记录
        /// </summary>
        /// <param name="input"></param>
        /// <param name="DeviceId"></param>
        /// <returns></returns>
        public  async Task<PageResult<Base_RepairOrderAccidentDTO>> GetRepairDeviceId(PageInput<PB_ScheduleDetailConditionDTO> input, string DeviceId)
        {
            var where = LinqHelper.True<Base_RepairOrderAccidentDTO>();
            var search = input.Search;
            Expression<Func<Base_RepairOrderAccident, Base_Department, Base_Device, Base_RepairOrderAccidentDTO>> select = (a, b, c) => new Base_RepairOrderAccidentDTO
            {
                DeviceName = c.Name,
                AccidentDepartmentName = b.Name,
            };
            select = select.BuildExtendSelectExpre();
            var q = from a in GetIQueryable().AsExpandable()
                    join b in Db.GetIQueryable<Base_Department>() on a.AccidentDepartment equals b.Id into ab
                    from b in ab.DefaultIfEmpty()
                    join c in Db.GetIQueryable<Base_Device>() on a.DeviceId equals c.Id into ac
                    from c in ac.DefaultIfEmpty()
                    select @select.Invoke(a, b, c);
            q = q.Where(m=>m.DeviceId==DeviceId);
            return await q.Where(where).GetPageResultAsync(input);
        }

        #endregion

        #region 私有成员

        #endregion
    }
}